using Engine;
using Engine.Graphics;
using Game;
using GameEntitySystem;

namespace SCIENEW {
    public class CheckValveDevice : SixDirectionModelDevice, IEntityDevice<ComponentCheckValve>, IFluidTransferrerDevice {
        public BlockMesh m_displayBlockMesh = new();

        public CheckValveDevice() : base("CheckValveDevice", "Models/CheckValve", "CheckValve", true) {
            m_displayBlockMesh.AppendBlockMesh(m_blockMeshesByFace[1]);
            m_displayBlockMesh.TransformPositions(Matrix.CreateTranslation(-0.5f, -0.5f, -0.5f));
        }

        public override bool IsTransparent_(int value) => true;
        
        public override int GetFaceTextureSlot(int face, int value) => 107;

        public override int GetDisplayOrder(int value) => base.GetDisplayOrder(value) + 1;

        public override DeviceBehavior[] GetBehaviors(Project project) => [new CheckValveDeviceBehavior(project), new FluidTransferrerDeviceBehavior(project)];

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData) {
            BlocksManager.DrawMeshBlock(
                primitivesRenderer,
                m_displayBlockMesh,
                IndustrialModLoader.IETexture,
                color,
                size,
                ref matrix,
                environmentData
            );
        }

        public FluidTransferrer? CreateFluidTransferrer(SubsystemFluidTransfer subsystemFluidTransfer, SubsystemTerrain subsystemTerrain, int value, Point3 point) => new CheckValveTransferrer(point, subsystemTerrain);

        public FluidConnector? GetConnector(SubsystemTerrain terrain, int value, int face, int x, int y, int z) {
            if (face == GetDirection_Static(value)) return new FluidConnector(new CellFace(x, y, z, face), FluidConnectorType.Output, 16);
            if (CellFace.OppositeFace(face) == GetDirection_Static(value)) return new FluidConnector(new CellFace(x, y, z, face), FluidConnectorType.Input, 16);
            return null;
        }

        public string GetEntityName(int value) => "CheckValve";

        public Widget GetWidget(IInventory inventory, ComponentCheckValve component) => new CheckValveWidget(inventory, component);

        public override int GetMaxStacking(int value) => 40;
    }
}